Convert Decimal into Binary using Recursion

Course- R Programming >

Source Code

# Program to convert decimal
# number into binary number
# using recursive function

binary <- function(n) {

    if(n > 1) {
        binary(as.integer(n/2))
    }
    cat(n %% 2)
}

Output


> binary(52)
110100

In this program, we convert decimal number entered by the user into binary using a recursive function. Decimal number is converted into binary by dividing the number successively by 2 and printing the remainder in reverse order.